﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.GeneralClass
{
    public class TaskDB
    {
        public int taskId;
        public string taskName;
        public DateTime startTime;
        public DateTime endTime;
        public int projectId;
        public string Discription;
    }


    public class OperateTask
    {
        MyDb mydb = Mdl_Class.Mydb;
        MyDb mydb_other = Mdl_Class.Mydb_other;
        //添加任务信息
        public string AddTask(TaskDB task)
        {
            string i = "";
            using (mydb)
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("insert into T_B_Task(taskName,projectId,startTime,endTime,Discription) values('{0}',{1},GetDate(),GetDate(),'{2}') Select @@Identity", task.taskName, task.projectId, task.Discription);

                    i = mydb.ExecuteScalar(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddTask(TaskDB task)", ex.ToString());
                }
            }

            return i;
        }

        //修改任务信息
        public int UpdateTask(int id)
        {
            int i = 0;
            using (mydb)
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("update T_B_Task set endTime=GetDate() where taskId={0}", id);

                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("UpdateTask(int id)", ex.ToString());
                }
            }

            return i;
        }



        //根据条件查询项目信息
        public List<TaskDB> GetTaskBySql(string taskName, int projectId,DateTime dt1,DateTime dt2,bool state)
        {
            List<TaskDB> tasklist = new List<TaskDB>();

            using (mydb)
            {
                try
                {
                    mydb.Open();



                    string sql = "select * from T_B_Task where 1=1 ";
                    if (projectId != 0)
                    {
                        sql = sql + string.Format(" and projectId={0}", projectId);
                    }
                    if (taskName != "")
                    {
                        sql = sql + string.Format(" and taskName='{0}'", taskName);
                    }

                    if (state)
                    {
                        sql = sql + string.Format(" and startTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        TaskDB task = new TaskDB();
                        task.taskId = Convert.ToInt32(mydb.DataReader["taskId"]);
                        task.taskName = mydb.DataReader["taskName"].ToString();
                        task.startTime = Convert.ToDateTime(mydb.DataReader["startTime"].ToString());
                        task.endTime = Convert.ToDateTime(mydb.DataReader["endTime"].ToString());
                        task.projectId = Convert.ToInt32(mydb.DataReader["projectId"]);
                        task.Discription = mydb.DataReader["Discription"].ToString();
                        tasklist.Add(task);
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetTaskBySql(string taskName, int projectId,DateTime dt1,DateTime dt2)", ex.ToString());
                }
            }
            return tasklist;
        }



    }
}
